package com.credit.service;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.credit.dao.admin.AdminUser;
import com.credit.dao.student.StudentUser;
import com.credit.dao.teacher.TeacherUser;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import java.util.Date;

/**
 * @Classname TokenService
 * @Description 生成token
 */
@Service("TokenService")
   public class TokenService   {
   /**
    * 获取管理员端 token
    * **/
   public String getAdminToken(AdminUser adminUser) {
      Date start = new Date();
      long currentTime = System.currentTimeMillis() + 60*60*1000; // 一小时有效时间
      Date end = new Date(currentTime);
      String token = "";

      token = JWT.create().withAudience(StringUtils.toString(adminUser.getZhanghao())).withIssuedAt(start).withExpiresAt(end)
              .sign(Algorithm.HMAC256(adminUser.getPassword()));
      return token;
   }


      /**
       * 获取教师端 token
       * **/
      public String getTeacherToken(TeacherUser teacherUser) {
         Date start = new Date();
         long currentTime = System.currentTimeMillis() + 60*60*1000; // 一小时有效时间
         Date end = new Date(currentTime);
         String token = "";

         token = JWT.create().withAudience(StringUtils.toString(teacherUser.getZhanghu())).withIssuedAt(start).withExpiresAt(end)
                 .sign(Algorithm.HMAC256(teacherUser.getPassword()));
         return token;
      }

   /**
    * 获取学生端 token
    * **/
   public String getStudentToken(StudentUser studentUser) {
      Date start = new Date();
      long currentTime = System.currentTimeMillis() + 60*60*1000; // 一小时有效时间
      Date end = new Date(currentTime);
      String token = "";

      token = JWT.create().withAudience(studentUser.getXuehao().toString()).withIssuedAt(start).withExpiresAt(end)
              .sign(Algorithm.HMAC256(studentUser.getPassword()));
      return token;
   }
   }
